<?php

namespace app\admin\controller\seo;

use app\common\controller\Backend;

/**
 * 关键词管理
 *
 * @icon fa fa-circle-o
 */
class Word extends Backend
{
    
    /**
     * Word模型对象
     * @var \app\admin\model\Word
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Word;
    }
    
    /**
     * 地区
     */
	public function index()
    {
		$adminID = $this->auth->id ;
        //设置过滤方法
    //    $this->request->filter(['strip_tags']);
		
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
         /*   if ($this->request->request('keyField')) {
                return $this->selectpage();
            }  */
			
          //  list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
            //    ->where($where)
				->where('uid','=',$adminID)
             //   ->order($sort, $order)
             //   ->limit($offset, $limit)
                ->select(); 
				
			//  找到单个字段  重新塞进二维数组$list
			//  类表形式
		    // $laist = array(array('prehead'=>'苏州','show'=>1),array('prehead'=>'上海','show'=>1));
		    $laist = $list[0]['prehead'];
			$laist = unserialize($laist);
			//$total = count($laist);
           //  $list = collection($laist)->toArray();
            $result = array("rows" => $laist);

            return json($result);  
		
        }
		
        return $this->view->fetch();
    }
	
	/**
     * 添加
	 * 地区
     */
    public function add()
    {
		//1.查看数据库UID 是否 存在
		//2.存在写入 二维数组
		//3.不存在新建存入 二维数组
		$adminID = $this->auth->id ;

        if ($this->request->isPost()) {

			$params = $this->request->post("row/a");
		
			$is_res = $this->model->where('uid','=',$adminID)->find();
			if(!empty($is_res)){
				//存在 更新
				if($is_res['prehead'] == NULL){
					//查看是否为空
					$arr = serialize(array(array('prehead'=>$params['prehead'],'show'=>1)));
					$result = $this->model->where('uid','=',$adminID)->setField('prehead',$arr);
					if ($result !== false) {
						$this->success();
					} else {
						$this->error($this->model->getError());
					} 
				}else{
					//反序列化
					$arr = unserialize($is_res['prehead']);
					//追加数组
					$info = array('prehead'=>$params['prehead'],'show'=>1);
					array_push($arr,$info);
					
					//更新数据库
					$arr = serialize($arr);
					$result = $this->model->where('uid','=',$adminID)->setField('prehead',$arr);
					if ($result !== false) {
						$this->success();
					} else {
						$this->error($this->model->getError());
					} 
				}	
			}else{
				//不存在 新建数据
				//@name 地区 show 是否显示
				//$array = array(array('name' => $params['prehead'] ,"show" => 1));
				//序列化字段  组成二维数组
				
				$list = serialize(array(array('prehead'=>$params['prehead'],'show'=>1)));
				$data = ['uid' => $adminID, 'prehead' => $list,'prefix' => '','main' => '','suffix'=>''];
				$result = $this->model->insert($data);
				
				if ($result !== false) {
                    $this->success();
                } else {
                    $this->error($this->model->getError());
                } 
			}  
        } 
        return $this->view->fetch();
    }
	
	
	/**
     * 编辑
     */
    public function edit($ids = NULL)
    {
		$adminID = $this->auth->id ;
		$ids = input('ids');
		//根据adminID 查到相关内容
		$is_res = $this->model->where('uid','=',$adminID)->find();
		$arr = unserialize($is_res['prehead']);
		//显示到页面上
		foreach($arr as $k=>$v){	
			if($v['prehead'] ==  $ids ){
				//匹配到了 显示到模板上
				$this->assign('arts',$arr[$k]);
			}	
		}
		if ($this->request->isPost()) { 
			$params = $this->request->post("row/a");
			$prehead = $params['prehead'];
			$show = input('show');

			if(!empty($prehead) and !empty($show)){
				foreach($arr as $k=>$v){	
					if($v['prehead'] ==  $ids ){
						//匹配到了 更新数据库
						$arr[$k] = array('prehead'=>$prehead,'show'=>$show);
					}	
				}
				$arr = serialize($arr);	
				//更新数据库
				$result = $this->model->where('uid','=',$adminID)->setField('prehead',$arr);
				if ($result !== false) {
					$this->success();
				} else {
					$this->error($this->model->getError());
				}
			}
		}
		return $this->fetch();
    }
	
	/**
     * 删除
     */
    public function del($ids = "")
    {
		//获取传过来的数据
		$ids = input('ids');
		$adminID = $this->auth->id ;
		$is_res = $this->model->where('uid','=',$adminID)->find();
		if(!empty($is_res)){
			//获取要操作的值  数组
			$arr = unserialize($is_res['prehead']);
			
			foreach($arr as $k=>$v){
				
				if($v['prehead'] ==  $ids ){
					unset($arr[$k]);
				}
			}
			//写入数据库
			
			$arr = serialize(array_values($arr));
			$result = $this->model->where('uid','=',$adminID)->setField('prehead',$arr);
			if ($result !== false) {
                $this->success();
            } else {
                $this->error($this->model->getError());
            } 
		}
    }
	
}
